Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-7584 HTMX extension for Ktor #4553

Merged
merged 4 commits into from
Feb 13, 2025
Merged

KTOR-7584 HTMX extension for Ktor #4553

merged 4 commits into from
Feb 13, 2025

Conversation

bjhham
Copy link
Contributor

@bjhham bjhham commented Dec 16, 2024

Subsystem
Server, New plugin

Motivation
KTOR-7584 First-class HTMX support

Solution
This extends the routing and HTML builder DSL with some handy HTMX helpers. I tried to ere on the conservative side here by not including everything and adding an experimental flag so we can change things later after testing. We don't have the HTML fragments extension yet in the HTML DSL, but you can still do everything you need using valid HTML and attributes.

@bjhham bjhham requested review from osipxd and Mr3zee December 16, 2024 13:34
Copy link
Member

@osipxd osipxd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally, it is here :)

Comment on lines 34 to 39
public val on: On
get() = On(map)

public fun on(event: String, script: String) {
map["hx-on:$event"] = script
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember what we decided here. Keep both options of syntax?

on["event] = "script"
on("event", "script")

public var pushUrl: String? by HxAttributeKeys.PushUrl
public var select: String? by HxAttributeKeys.Select
public var selectOob: String? by HxAttributeKeys.SelectOob
public var swap: String? by HxAttributeKeys.Swap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've had type-safe implementations for some of these fields. Will it be part of some future PR?

@osipxd osipxd force-pushed the 3.1.0-eap branch 2 times, most recently from 7c22c8a to f555d1a Compare December 19, 2024 10:14
@osipxd osipxd force-pushed the 3.1.0-eap branch 5 times, most recently from 2c3b4d8 to 4a59355 Compare January 9, 2025 14:59
Base automatically changed from 3.1.0-eap to main January 9, 2025 15:50
@bjhham bjhham requested a review from osipxd February 10, 2025 13:13
/**
* Sub-routes only apply to a specific HX-Target header.
*/
public fun target(expectedTrigger: String, body: Route.() -> Unit): Route =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public fun target(expectedTrigger: String, body: Route.() -> Unit): Route =
public fun target(expectedTarget: String, body: Route.() -> Unit): Route =

}

/**
* Simplifies property access delegation for HxAttributes when setting attribute values from a string constant.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor. Probably we could drop "HxAttributes" from the description?

@bjhham bjhham force-pushed the bjhham/htmx-ext branch 2 times, most recently from 84bbd03 to 6c7cca9 Compare February 10, 2025 16:49
@bjhham bjhham changed the base branch from main to 3.2.0-eap February 13, 2025 07:45
@bjhham bjhham merged commit 985f31c into 3.2.0-eap Feb 13, 2025
13 of 17 checks passed
@bjhham bjhham deleted the bjhham/htmx-ext branch February 13, 2025 12:45
@ulrikandersen
Copy link

This is amazing. Nice work! 🎊

We don't have the HTML fragments extension yet in the HTML DSL

@bjhham Do you know if that is being tracked anywhere? It would be really useful with the HTMX plugin where we often respond with just a div etc.

@osipxd
Copy link
Member

osipxd commented Feb 14, 2025

Do you know if that is being tracked anywhere?

Here it is: KTOR-8195 Partial HTML response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants